package com.cvmars.jianyou.module.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.api.model.UserMode;
import com.cvmars.jianyou.ui.CoutDownTextView;
import com.cvmars.jianyou.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResetPasswordActivity extends AppCompatActivity {

    @BindView(R.id.txt_reset_phone)
    EditText txtResetPhone;
    @BindView(R.id.txt_reset_password)
    EditText txtResetPassword;
    @BindView(R.id.txt_reset_code)
    EditText txtResetCode;
    @BindView(R.id.txt_reset_btn)
    Button txtResetBtn;
    @BindView(R.id.btn_count_code)
    CoutDownTextView btnCountCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        ButterKnife.bind(this);
        btnCountCode.setOnClick(new CoutDownTextView.OnClick() {
            @Override
            public void onClick() {
                onTimeDownClicked();
            }
        });
    }

    public void onTimeDownClicked() {

        String phone = txtResetPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.show("手机号不能为空");
            return;
        }

        if (phone.length() != 11) {
            ToastUtils.show("手机号格式不正确");
            return;
        }

        HttpUtils.getInstance().toSubscribe(Api.getInstance().postRegisterSms(phone)
                , new SimpleSubscriber<HttpResult<Object>>() {
                    @Override
                    protected void _onNext(HttpResult<Object> o) {

                        ToastUtils.show(o.getMsg());
                        btnCountCode.onRequest();
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }

    @OnClick(R.id.txt_reset_btn)
    public void onViewClicked() {
        String phone = txtResetPhone.getText().toString();
        String password = txtResetPassword.getText().toString();
        String code = txtResetCode.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.show("手机号不能为空");
            return;
        }

        if (phone.length() != 11) {
            ToastUtils.show("手机号格式不正确");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtils.show("密码不能为空");
            return;
        }

        if (TextUtils.isEmpty(code)) {
            ToastUtils.show("验证码不能为空");
            return;
        }

        HttpUtils.getInstance().toSubscribe(Api.getInstance().postResetCodePassword(phone, password, code)
                , new SimpleSubscriber<HttpResult<UserMode>>() {
                    @Override
                    protected void _onNext(HttpResult<UserMode> o) {

                        ToastUtils.show(o.getMsg());

                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }

    private void onLogin() {

        String phone = txtResetPhone.getText().toString();
        String password = txtResetPassword.getText().toString();


        HttpUtils.getInstance().toSubscribe(Api.getInstance().postLogin(phone, password)
                , new SimpleSubscriber<HttpResult<UserMode>>() {
                    @Override
                    protected void _onNext(HttpResult<UserMode> o) {

                        ToastUtils.show(o.getMsg());


                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });

    }
}
